package org.unt.mail.sender;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;

public class MailSender {

	private static Logger logger = Logger.getLogger("mail");

	public static void main(String[] args) {

		String path = System.getProperty("java.library.path");

		String[] paths = path.split(";");
		for (int i = 0; i < paths.length; i++) {
			System.out.println(paths[i]);
		}

		if (true)
			return;

		MessageBean mail = new MessageBean("rafaelgramajo@gmail.com", "rafaelgramajo@hotmail.com", "subject", "text!");

		sendMail(mail);
	}

	public static void sendMail(MessageBean messageBean) {

		// Properties
		Properties props = new Properties();
		props.setProperty("mail.smtp.host", "smtp.gmail.com");
		props.setProperty("mail.debug", "false");
		props.setProperty("mail.smtp.starttls.enable", "true");

		props.setProperty("mail.smtp.port", "587");
		// props.put("mail.smtp.port", "465");
		props.setProperty("mail.from", "rafaelgramajo@gmail.com");

		Session session = Session.getDefaultInstance(props);

		MimeMessage message = new MimeMessage(session);

		try {

			message.setFrom(new InternetAddress(messageBean.getFrom()));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(messageBean.getTo()));
			message.setSubject(messageBean.getSubject());
			// message.setText(messageBean.getText());

			// Attachment
			MimeBodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setText(messageBean.getText());

			Multipart multipart = new MimeMultipart();
			multipart.addBodyPart(messageBodyPart);

			// Part two is attachment
			// messageBodyPart = new MimeBodyPart();
			// DataSource source = new FileDataSource(attachment);
			// messageBodyPart.setDataHandler(new DataHandler(source));
			// messageBodyPart.setFileName(attachment.getName());
			// multipart.addBodyPart(messageBodyPart);

			message.setContent(multipart);

			Transport t = session.getTransport("smtp");

			t.connect("rafaelgramajo@gmail.com", "agdmbcchnmm");

			t.sendMessage(message, message.getAllRecipients());
			logger.info("Message sent to " + messageBean.getTo());

		} catch (AddressException e) {
			// logger.error(ThrowableUtil.toString(e));
			e.printStackTrace();
			// logger.error("Message not sent: " + MailUtil.toString(message));
		} catch (MessagingException e) {
			// logger.error(ThrowableUtil.toString(e));
			// logger.error("Message not sent: " + MailUtil.toString(message));
			e.printStackTrace();
		}
	}
	/*
	 * public static void sendMail(MessageBean messageBean) {
	 * 
	 * // Properties Properties props = new Properties(); props.setProperty("mail.smtp.host", PropsUtil.getProp("mail.smtp.host"));
	 * props.setProperty("mail.smtp.auth", PropsUtil.getProp("mail.smtp.auth")); props.setProperty("mail.debug", PropsUtil.getProp("mail.debug"));
	 * props.setProperty("mail.smtp.starttls.enable", PropsUtil.getProp("mail.smtp.starttls.enable"));
	 * 
	 * // props.setProperty("mail.smtp.port", "587"); props.setProperty("mail.from", PropsUtil.getProp("mail.user.login"));
	 * 
	 * Session session = Session.getDefaultInstance(props); MimeMessage message = new MimeMessage(session);
	 * 
	 * try {
	 * 
	 * message.setFrom(new InternetAddress(messageBean.getFrom())); message.addRecipient(Message.RecipientType.TO, new InternetAddress(messageBean.getTo()));
	 * message.setSubject(messageBean.getSubject()); message.setText(messageBean.getText());
	 * 
	 * Transport t = session.getTransport("smtp");
	 * 
	 * if (Boolean.parseBoolean(PropsUtil.getProp("mail.smtp.auth"))) { t.connect(PropsUtil.getProp("mail.user.login"), PropsUtil.getProp("mail.pass.login")); }
	 * else { t.connect(); }
	 * 
	 * t.sendMessage(message, message.getAllRecipients()); logger.info("Message sent to " + messageBean.getTo());
	 * 
	 * } catch (AddressException e) { // logger.error(ThrowableUtil.toString(e)); logger.error("Message not sent: " + MailUtil.toString(message)); } catch
	 * (MessagingException e) { // logger.error(ThrowableUtil.toString(e)); logger.error("Message not sent: " + MailUtil.toString(message)); } }
	 * 
	 * public static void queueMailToAdmin(String subject, String text) { queueMail(PropsUtil.getProp("mail.true.admin"), subject, text); }
	 * 
	 * public static void queueMail(String to, String subject, String text) { MailSenderThread.queueMessageToSend(new
	 * MessageBean(PropsUtil.getProp("mail.from.fantasy"), to, subject, text)); }
	 * 
	 * public static void queueMail(String to, String subject, String text, File attachment) {
	 * 
	 * MessageBean message = new MessageBean(PropsUtil.getProp("mail.from.fantasy"), to, subject, text);
	 * 
	 * message.setAttachment(attachment);
	 * 
	 * MailSenderThread.queueMessageToSend(message); }
	 */
}
